package cn.edu.sicnu.cardgame

class CardMatchingGame(val count: Int) {
    var score = 0
        private set

    val cards: MutableList<Card>

    init {
        val deck = Deck()
        cards = mutableListOf()
        for (i in 1..count) {
            val card: Card? = deck.drawRandomCard()
            if (card != null) {
                cards.add(card)
            }
        }
    }

    fun reset() {
        val deck = Deck()
        cards.clear()
        score = 0
        for (i in 1..count) {
            val card: Card? = deck.drawRandomCard()
            if (card != null) {
                cards.add(card)
            }
        }
    }

    fun cardAtIndex(index: Int): Card {
        return cards.get(index)
    }

    val MISMATCH_PENALTY = 2
    val MATCH_BONUS = 4
    val COST_TO_CHOOSE = 1

    fun chooseCardAtIndex(index: Int) {
        val card = cardAtIndex(index)
        if (!card.isMatched) {
            if (card.isChosen) {
                card.isChosen = false
            } else {
                //                //两张
////                for (otherCard in cards) {
////                    if (otherCard.isChosen && !otherCard.isMatched) {
////                        val matchScore = card.match(arrayOf(otherCard))
////                        if (matchScore > 0) {
////                            score += matchScore * MATCH_BONUS
////                            otherCard.isMatched = true
////                            card.isMatched = true
////                        } else {
////                            score -= MISMATCH_PENALTY
////                            otherCard.isChosen = false
////                        }
////                        break
////                    }
////                }
//                //三张
                for (otherCard in cards) {
                    if (otherCard.isChosen && !otherCard.isMatched) {
                        var matchScore = card.match(arrayOf(otherCard))
                        if (matchScore > 0) {
                            for (otherCard1 in cards) {
                                if (otherCard1.isChosen && !otherCard1.isMatched && otherCard1 != otherCard) {
                                    var matchScore1 = card.match(arrayOf(otherCard1))
                                    if (matchScore1 > 0) {
                                        score += matchScore1 * MATCH_BONUS
                                        otherCard1.isMatched = true
                                        otherCard.isMatched = true
                                        card.isMatched = true
                                    } else {
                                        otherCard.isChosen = false
                                        otherCard1.isChosen = false
                                    }
                                    break
                                }
                            }
                        } else {
                            score -= MISMATCH_PENALTY
                            for (c in cards) {
                                if (c.isChosen && !c.isMatched) {
                                    c.isChosen = false
                                }
                            }
                        }
                        break
                    }
                }
                score -= COST_TO_CHOOSE
                card.isChosen = true
            }
        }
    }
}